import { reactive, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue';
import page from './page.ts';
import { useRouter, useRoute } from 'vue-router';
export function useListComponent() {
  const router = useRouter();
  const route = useRoute();
  const { proxy } = getCurrentInstance() as any;
  //页面信息
  const dataInfo: any = reactive({
    loading: false, //加载
    loadingMsg: '列表数据加载中', //加载
    table: {
      title: page.title, //表格-标题
      //表格-内容数据
      tbody: [],
      rowKey: 'productMapQuickOrderingID',
      theadCode: 'customerlist', //basedatatable
      //表格-表头数据
      thead: [],
      //表格-搜索数据
      search: {
        pageIndex: 1, //当前页码
        pageSize: 30, //每页显示数据的数量
        totalCount: 0, //总数量
        queryDataBaseFiled: '', // 关键字查询字段，空值查询全部
        likeWords: '', // 查询关键字，空值查询全部
        beginTime: '', // 日期范围-开始时间
        endTime: '', // 日期范围-结束时间
        onlySelf: 0, // 是否只查询自己创建的，0：查所有、1：只查自己创建的
        isShowDisabled: 0, // 是否显示禁用,-1：显示所有的；0：仅显示未作废的；1 仅显示作废的
        orderByFiled: '', // 排序字段
        orderByType: 0, // 排序方式，0-倒序,1-升序
        settlementWay: 0, // 结算方式：1日结；2月结，0查询全部
        belongToSalesman: '', // 销售代表代码，空值查询全部
        isEnabled: -1, // 是否启用：-1：查全部；1只查启用的；0只查禁用的
      },
      //多选
      checkbox: {
        state: true,
        selectable: (row: any) => {
          return true;
        },
        list: [],
      },
      //表格-按钮数据
      btns: [
        {
          title: '导出数据',
          icon: 'Download',
          type: '',
          children: [
            {
              title: '导出当页数据',
              click: () => {
                setLoading('导出当页数据,请稍等');
                exportBtn(dataInfo.table.tbody);
              },
            },
            {
              title: '导出全部数据',
              click: () => {
                setLoading('正在导出全部数据,请稍等');
                proxy.$api.OrderBusniess.QueryPageCustomerList({
                  ...dataInfo.table.search,
                  pageIndex: 1,
                  pageSize: 10000,
                  beginTime: dataInfo.table.search.beginTime || proxy.$getTimeAgo(1, 'years'), // 开始时间
                  endTime: dataInfo.table.search.endTime || proxy.$day(new Date()), // 结束时间
                })
                  .then((res: any) => {
                    exportBtn(res.list);
                  })
                  .catch(() => {
                    dataInfo.loading = false;
                  });
              },
            },
            {
              title: '导出选中数据',
              click: () => {
                setLoading('导出选中数据,请稍等');
                exportBtn(dataInfo.table.checkbox.list);
              },
            },
          ],
        },
        {
          title: '新建',
          icon: 'Plus',
          type: 'primary',
          permission: 'MenuCode1-4-1',
          click: () => {
            getPath({
              path: `${route.path}/add`,
              query: {
                type: 1,
              },
            });
          },
        },
      ],
      //表格-列表搜索
      operation: [
        {
          title: '编辑',
          permission: 'MenuCode1-4-1',
          click: (row: any) => {
            getPath({
              path: `${route.path}/details`,
              query: {
                type: 2,
                customerID: row.customerID,
              },
            });
          },
        },
        {
          title: (row: any) => {
            return row.customerState === 1 ? '禁用' : '启用';
          },
          permission: 'MenuCode1-4-1',
          click: (row: any, index: number) => {
            proxy.$api.OrderBusniess.DisableorEnableCustomer({
              customerID: row.customerID,
              isEnabled: row.customerState === 0,
            })
              .then((res: any) => {
                proxy.$message(`${row.customerState === 1 ? '禁用' : '启用'}成功`);
                getTbody();
              })
              .catch(() => {});
          },
        },
        {
          title: '删除',
          permission: 'MenuCode1-4-3',
          click: (row: any, index: number) => {
            proxy.$messageBox('是否删除当前数据，请确认！', () => {
              proxy.$api.OrderBusniess.DeleteCustomer({
                customerID: row.customerID,
              })
                .then((res: any) => {
                  proxy.$message(`删除成功`);
                  getTbody();
                })
                .catch(() => {});
            });
          },
        },
      ],
    },
  });
  const imageViewer: any = reactive({
    show: false,
    list: [],
  });
  const options = reactive({
    //结算方式
    settlementWay: [
      {
        label: '月结',
        value: 2,
        type: 'success',
      },
      {
        label: '现结',
        value: 1,
      },
    ],
    //启用状态
    isEnabled: [
      {
        label: '启用',
        value: 1,
        type: 'success',
      },
      {
        label: '禁用',
        value: 0,
        type: 'info',
      },
    ],
    //是否含税
    isIncludingTax: [
      {
        label: '未税',
        value: 0,
        type: 'info',
      },
      {
        label: '含税',
        value: 1,
        type: 'success',
      },
    ],
    personnel: [], //员工
    treeArea: [], //地区
  });
  // 获取tabs数据
  const getPath = (obj: any, row?: any) => {
    const path = router.resolve(obj);
    // 在新窗口或标签页中打开目标路径
    const newWindow: any = window.open(path.href, '_blank');
    newWindow;
  };
  // 获取表数据
  const getTbody = () => {
    let _table: any = dataInfo.table;
    setLoading();
    proxy.$api.OrderBusniess.QueryPageCustomerList(_table.search)
      .then((res: any) => {
        _table.tbody = res.list;
        _table.search.totalCount = res.totalCount;
        dataInfo.loading = false;
      })
      .catch(() => {
        dataInfo.loading = false;
      });
  };
  // 导出数据
  const exportBtn = (tbody: any[]) => {
    proxy.$exportData(
      {
        thead: dataInfo.table.thead,
        tbody: tbody,
        fileName: dataInfo.table.title,
      },
      dataInfo,
    );
    dataInfo.loading = false;
  };
  const setLoading = (title: string = '正在获取数据,请稍等...') => {
    if (!dataInfo.loading) {
      dataInfo.loading = true;
      dataInfo.loadingMsg = title;
    }
  };
  // 组件挂载后执行
  onMounted(() => {
    getTbody();
  });
  // 组件卸载前执行
  onBeforeUnmount(() => {});
  return { dataInfo, getTbody, options, imageViewer };
}
